package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.TreeNode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

/**
 * @ClassName InvertTree
 * @Description InvertTree
 * @Author wanghaiwei
 * @Date 2023/1/16 10:56
 */
public class InvertTree {
    public TreeNode invertTree(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(tmp);
        return root;
    }
    public static void main(String[] args) {
        InvertTree solution = new InvertTree();
        TreeNode root1 = new TreeNode(4);
        root1.left = new TreeNode(2);
        root1.left.left = new TreeNode(1);
        root1.left.right = new TreeNode(3);
        root1.right = new TreeNode(7);
        root1.right.left = new TreeNode(6);
        root1.right.right = new TreeNode(9);
        String result = StringUtils.join(TreeNode.bfs(solution.invertTree(root1)), ',');
        Assert.isTrue("4,7,2,9,6,3,1".equals(result), "[4,2,7,1,3,6,9]计算错误,实际结果:"+result);
    }
}
